﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace VMDS.GUIClient.Helper
{
    public static class WebBrowserUtility
    {
        public static readonly DependencyProperty BindableSourceProperty =
            DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged));

        public static string GetBindableSource(DependencyObject obj)
        {
            return (string)obj.GetValue(BindableSourceProperty);
        }

        public static void SetBindableSource(DependencyObject obj, string value)
        {
            obj.SetValue(BindableSourceProperty, value);
        }

        public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            WebBrowser browser = o as WebBrowser;
            if (browser != null)
            {
                try
                {
                    string uri = (e.NewValue as string);
                    if (uri[0] == '\\') uri = "\\" + uri;
                    browser.Source=new Uri(Environment.CurrentDirectory+ "\\check.htm");
                    DirectoryInfo info = new DirectoryInfo(uri);
                    if (!info.Exists) throw new DirectoryNotFoundException();
                    browser.Source = new Uri(uri);
                }
                catch (Exception ex)
                {
                    if (ex is NullReferenceException) browser = new WebBrowser();
                    else if (ex is DirectoryNotFoundException)
                    browser.Source = new Uri(Environment.CurrentDirectory+ "\\404.htm");
                    else browser.Source = new Uri(Environment.CurrentDirectory + "\\403.htm");
                }
            }
        }

    }

}
